#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年5月16日

@author: Administrator
'''
from article import settings
from article.models import Article
from article.tasks import update_article_to_es_task
import logging

# 为loggers中定义的名称
logger = logging.getLogger("celery")
 
def update_article_to_es(articles):
    '''
    
    '''
    #判断articles类型
    if isinstance(articles, Article):
        articles = [articles]
        
    for article in articles:
        #构造文章json字符串
        logger.info(u"start make article es source by id %s " % article.id)    
        _source = {
            "id":article.id,
            "create_time" : article.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "modify_time" : article.modify_time.strftime("%Y-%m-%d %H:%M:%S"),
            "state" : article.state,
            "state_desc" : settings.get_state_desc(article.state),
            "topic" : article.topic,
            "user_id" : article.user_id,
            "user_name" : article.user.first_name or article.user.username,
            "content" : article.content,
            "text_content" : article.text_content,
            "source" : article.source,
            "source_desc" : settings.SOURCE_CHOICES[article.source][1],
            "source_id" : article.source_id,
        }
        update_article_to_es_task.delay(_source)
        logger.info(u"end make article es source by id %s " % article.id)    
